package pers.yurwisher.rubick.manager.controller;

import lombok.RequiredArgsConstructor;
import org.bouncycastle.util.Selector;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.yurwisher.rubick.common.base.BaseController;
import pers.yurwisher.rubick.common.wrapper.PageR;
import pers.yurwisher.rubick.common.wrapper.R;
import pers.yurwisher.rubick.manager.entity.SystemRole;
import pers.yurwisher.rubick.manager.pojo.fo.SystemRoleFo;
import pers.yurwisher.rubick.manager.pojo.qo.SystemRoleQo;
import pers.yurwisher.rubick.manager.pojo.to.SystemRoleTo;
import pers.yurwisher.rubick.manager.pojo.tree.MenuAndButtonTreeNode;
import pers.yurwisher.rubick.manager.service.ISystemRoleService;

import java.util.List;

/**
 * @author yq
 * @date 2020-09-21 14:45:55
 * @description 角色
 * @since V1.0.0
 */
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class SystemRoleController extends BaseController {
    private final ISystemRoleService systemRoleService;

    @PostMapping
    public R<String> create(@RequestBody SystemRoleFo fo) {
        systemRoleService.create(fo);
        return R.ok();
    }

    @PostMapping("{id}")
    public R<String> update(@PathVariable(name = "id") Long id, @RequestBody SystemRoleFo fo) {
        systemRoleService.update(id, fo);
        return R.ok();
    }

    @GetMapping("{id}")
    public R<SystemRole> get(@PathVariable(name = "id") Long id) {
        return R.ok(systemRoleService.getById(id));
    }

    @PostMapping("/delete/{id}")
    public R<String> delete(@PathVariable(name = "id") Long id) {
        systemRoleService.delete(id);
        return R.ok();
    }

    @GetMapping
    public R<PageR<SystemRoleTo>> list(@ModelAttribute SystemRoleQo qo) {
        return R.ok(systemRoleService.list(qo));
    }

    @GetMapping("select")
    public R<List<Selector<Long>>> select() {
        return R.ok(systemRoleService.select());
    }

    @PostMapping("/{roleId}/bindMenuAndButton")
    public R<String> bindMenuAndButton(@RequestBody List<MenuAndButtonTreeNode> nodes, @PathVariable Long roleId) {
        systemRoleService.bindMenuAndButton(roleId, nodes);
        return R.ok();
    }

    @GetMapping("/{roleId}/singleRoleMenuAndButton")
    public R<List<String>> singleRoleMenuAndButton(@PathVariable Long roleId) {
        return R.ok(systemRoleService.singleRoleMenuAndButton(roleId));
    }

}
